package top.drinyeit.weilong.retrofit;


import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import top.drinyeit.weilong.model.AddressBean;
import top.drinyeit.weilong.model.AppHomeBean;
import top.drinyeit.weilong.model.AppraiseCommodityModel;
import top.drinyeit.weilong.model.AuthenticationDetailBean;
import top.drinyeit.weilong.model.BalanceDetailBean;
import top.drinyeit.weilong.model.CartListBean;
import top.drinyeit.weilong.model.CategoryGoodsDetailsBean;
import top.drinyeit.weilong.model.CategoryStoreBean;
import top.drinyeit.weilong.model.ChildrenOrderBean;
import top.drinyeit.weilong.model.ChildrenOrderDetailBean;
import top.drinyeit.weilong.model.ClassificationBean;
import top.drinyeit.weilong.model.MCartListBean;
import top.drinyeit.weilong.model.MessageBean;
import top.drinyeit.weilong.model.OrderBean;
import top.drinyeit.weilong.model.OrderChildListBean;
import top.drinyeit.weilong.model.OrderFatherListBean;
import top.drinyeit.weilong.model.StoreHomeBean;
import top.drinyeit.weilong.model.StoreInfo;
import top.drinyeit.weilong.model.TemporaryOrderDetailBean;
import top.drinyeit.weilong.model.UserInfBean;
import top.drinyeit.weilong.model.WithdrawDetailBean;
import top.drinyeit.weilong.model.WxBean;

import static top.drinyeit.weilong.app.Constant.BASE_URL;

/**
 * Created by YJY on 2017/10/13.
 */
public class RetrofitMethod extends RxMethod {

    private RetrofitInterface retrofitInterface;

    /**
     * 获取RetrofitHelper对象的单例
     */
    private static class Singleton {
        private static final RetrofitMethod INSTANCE = new RetrofitMethod();
    }

    public static RetrofitMethod getInstance() {
        return Singleton.INSTANCE;
    }

    private RetrofitMethod() {
        Retrofit retrofit = new Retrofit.Builder()
                .client(OkHttpManager.getClient().getOkHttp())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
        retrofitInterface = retrofit.create(RetrofitInterface.class);
    }

    //登录/注册
    public void login(Subscriber<HttpResponse<String>> subscriber, String login_phone, String code) {
        Observable<HttpResponse<String>> observable = retrofitInterface.login("user_register_or_login", login_phone, code);
        onSubscribe(observable, subscriber);
    }

    //获取用户个人信息
    public void getInf(Subscriber<HttpResponse<UserInfBean>> subscriber, String user_id) {
        Observable<HttpResponse<UserInfBean>> observable = retrofitInterface.get_Inf("user_get_info", user_id);
        onSubscribe(observable, subscriber);
    }

    //编辑用户信息
    public void editInf(Subscriber<HttpResponse<String>> subscriber, Map<String,Object> map) {
        Observable<HttpResponse<String>> observable = retrofitInterface.edit_Inf(map);
        onSubscribe(observable, subscriber);
    }

    //获取收货地址
    public void getAdd(Subscriber<HttpResponse<List<AddressBean>>> subscriber, String user_id, int pageIndex) {
        Observable<HttpResponse<List<AddressBean>>> observable = retrofitInterface.get_add("user_shopping_address", user_id, pageIndex);
        onSubscribe(observable, subscriber);
    }

    //新增收货地址
    public void addNewAdd(Subscriber<HttpResponse<String>> subscriber, String user_id, String consignee_name, String consignee_phone, String address_detail,String longitude,String latitude) {
        Observable<HttpResponse<String>> observable = retrofitInterface.add_new_add("user_increase_address", user_id, consignee_name, consignee_phone, address_detail,longitude,latitude);
        onSubscribe(observable, subscriber);
    }

    //删除收货地址
    public void deleteAdd(Subscriber<HttpResponse<String>> subscriber, int address_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.delete_add("user_delete_address", address_id);
        onSubscribe(observable, subscriber);
    }

    //编辑收货地址
    public void editAdd(Subscriber<HttpResponse<String>> subscriber, int address_id, String consignee_name, String consignee_phone, String address_detail,String longitude,String latitude) {
        Observable<HttpResponse<String>> observable = retrofitInterface.edit_add("user_edit_address", address_id, consignee_name, consignee_phone, address_detail,longitude,latitude);
        onSubscribe(observable, subscriber);
    }

    //设置默认地址
    public void setDe(Subscriber<HttpResponse<String>> subscriber, String user_id, int address_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.set_de("user_default_address", user_id, address_id);
        onSubscribe(observable, subscriber);
    }

    //获取消息列表
    public void getMessage(Subscriber<HttpResponse<List<MessageBean>>> subscriber, String user_id,int pageIndex) {
        Observable<HttpResponse<List<MessageBean>>> observable = retrofitInterface.get_message("user_message", user_id, pageIndex);
        onSubscribe(observable, subscriber);
    }

    //标记消息已读
    public void setRead(Subscriber<HttpResponse<String>> subscriber, int message_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.set_read("user_modify_message_state", message_id);
        onSubscribe(observable, subscriber);
    }

    //删除用户消息
    public void deleteMessage(Subscriber<HttpResponse<String>> subscriber, int message_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.delete_message("user_delete_message", message_id);
        onSubscribe(observable, subscriber);
    }

    //获取用户当前余额
    public void getBalance(Subscriber<HttpResponse<Double>> subscriber, String user_id) {
        Observable<HttpResponse<Double>> observable = retrofitInterface.get_balance("user_balance", user_id);
        onSubscribe(observable, subscriber);
    }

    //获取用户余额收支明细
    public void getBlDe(Subscriber<HttpResponse<List<BalanceDetailBean>>> subscriber, String user_id) {
        Observable<HttpResponse<List<BalanceDetailBean>>> observable = retrofitInterface.get_bl_de("user_balance_detail", user_id, 1, 999);
        onSubscribe(observable, subscriber);
    }

    //获取用户提现明细
    public void getWithdrawDetail(Subscriber<HttpResponse<List<WithdrawDetailBean>>> subscriber, Map<String,Object> map) {
        Observable<HttpResponse<List<WithdrawDetailBean>>> observable = retrofitInterface.get_w_de(map);
        onSubscribe(observable, subscriber);
    }

    //z帐号充值
    public void recharge(Subscriber<HttpResponse<String>> subscriber, String user_id, String card_number, String card_password) {
        Observable<HttpResponse<String>> observable = retrofitInterface.recharge("user_account_recharge", user_id, card_number, card_password);
        onSubscribe(observable, subscriber);
    }

    //账号余额提现
    public void withdraw(Subscriber<HttpResponse<String>> subscriber, String user_id, String account_number, String account_name, String account_type, String amount) {
        Observable<HttpResponse<String>> observable = retrofitInterface.withdraw("user_balance_encashment", user_id, account_number, account_name, account_type, amount);
        onSubscribe(observable, subscriber);
    }

    //获取企业认证详情
    public void getCompany(Subscriber<HttpResponse<AuthenticationDetailBean>> subscriber, String user_id) {
        Observable<HttpResponse<AuthenticationDetailBean>> observable = retrofitInterface.get_company("company_authentication_detail", user_id);
        onSubscribe(observable, subscriber);
    }

    //修改企业认证
    public void editCompany(Subscriber<HttpResponse<String>> subscriber, Map<String,Object> map) {
        Observable<HttpResponse<String>> observable = retrofitInterface.edit_company(map);
        onSubscribe(observable, subscriber);
    }

    //系统
    //获取验证码
    public void get_code(Subscriber<HttpResponse<String>> subscriber, String login_phone) {
        Observable<HttpResponse<String>> observable = retrofitInterface.get_code("system_send_code", login_phone);
        onSubscribe(observable, subscriber);
    }

    //商城
    //app首页信息
    public void getApp(Subscriber<HttpResponse<AppHomeBean>> subscriber) {
        Observable<HttpResponse<AppHomeBean>> observable = retrofitInterface.get_app("commodity_home");
        onSubscribe(observable, subscriber);
    }

    //获取所有分类信息
    public void getCla(Subscriber<HttpResponse<ClassificationBean>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<ClassificationBean>> observable = retrofitInterface.get_cla(map);
        onSubscribe(observable, subscriber);
    }

    //查看分类商品
    public void categorySearch(Subscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<List<CategoryGoodsDetailsBean>>> observable = retrofitInterface.categorySearch(map);
        onSubscribe(observable, subscriber);
    }

    //店铺搜索
    public void categoryStoreSearch(Subscriber<HttpResponse<List<CategoryStoreBean>>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<List<CategoryStoreBean>>> observable = retrofitInterface.categoryStoreSearch(map);
        onSubscribe(observable, subscriber);
    }

    //获取商品详情
    public void getCommodityDetail(Subscriber<HttpResponse<CategoryGoodsDetailsBean>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<CategoryGoodsDetailsBean>> observable = retrofitInterface.commodity_detail(map);
        onSubscribe(observable, subscriber);
    }

    //获取店铺首页
    public void getStoreHome(Subscriber<HttpResponse<List<StoreHomeBean>>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<List<StoreHomeBean>>> observable = retrofitInterface.get_store_home(map);
        onSubscribe(observable, subscriber);
    }

    //获取全部宝贝
    public void getStoreAll(Subscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<List<CategoryGoodsDetailsBean>>> observable = retrofitInterface.get_store_all(map);
        onSubscribe(observable, subscriber);
    }

    //店铺信息
    public void getStoreInfo(Subscriber<HttpResponse<StoreInfo>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<StoreInfo>> observable = retrofitInterface.get_store_info(map);
        onSubscribe(observable, subscriber);
    }


    //获取子订单列表
    public void getChildrenOrderList(Subscriber<HttpResponse<List<ChildrenOrderBean>>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<List<ChildrenOrderBean>>> observable = retrofitInterface.get_children_order_list(map);
        onSubscribe(observable, subscriber);
    }

    //添加到购物车
    public void addCart(Subscriber<HttpResponse<String>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<String>> observable = retrofitInterface.add_cart(map);
        onSubscribe(observable, subscriber);
    }


    //获取子订单详情
    public void getCOD(Subscriber<HttpResponse<ChildrenOrderDetailBean>> subscriber, String order_id) {
        Observable<HttpResponse<ChildrenOrderDetailBean>> observable = retrofitInterface.get_children_order_detail("commodity_order_detail", order_id);
        onSubscribe(observable, subscriber);
    }

    //生成订单
    public void addOrder(Subscriber<HttpResponse<OrderBean>> subscriber, Map<String, Object> map) {
        Observable<HttpResponse<OrderBean>> observable = retrofitInterface.add_order(map);
        onSubscribe(observable, subscriber);
    }


    //购物车列表
    public void cartList(Subscriber<HttpResponse<CartListBean>> subscriber, String user_id) {
        Observable<HttpResponse<CartListBean>> observable = retrofitInterface.cartList("commodity_shopping_cart_list", user_id);
        onSubscribe(observable, subscriber);
    }

    //分类购物车列表
    public void categorCartList(Subscriber<HttpResponse<MCartListBean>> subscriber, String user_id) {
        Observable<HttpResponse<MCartListBean>> observable = retrofitInterface.categorCartList("commodity_shopping_cart_list", user_id);
        onSubscribe(observable, subscriber);
    }

    //删除购物车
    public void deleteCart(Subscriber<HttpResponse<String>> subscriber, String ids) {
        Observable<HttpResponse<String>> observable = retrofitInterface.deleteCart("commodity_delete_shopping_cart", ids);
        onSubscribe(observable, subscriber);
    }

    //更改购物车商品数量
    public void changeCartNumber(Subscriber<HttpResponse<String>> subscriber, String id, int commodity_sum) {
        Observable<HttpResponse<String>> observable = retrofitInterface.changeCartNumber("commodity_update_shopping_cart", id, commodity_sum);
        onSubscribe(observable, subscriber);
    }

    //购物车下单
    public void cartAccount(Subscriber<HttpResponse<TemporaryOrderDetailBean>> subscriber, String temp_order_params) {
        Observable<HttpResponse<TemporaryOrderDetailBean>> observable = retrofitInterface.cartAccount("commodity_temporary_order", temp_order_params);
        onSubscribe(observable, subscriber);
    }

    //获取父订单列表
    public void getFatherOrderList(Subscriber<HttpResponse<List<OrderFatherListBean>>> subscriber, String user_id, int state_flag, int pageIndex) {
        Observable<HttpResponse<List<OrderFatherListBean>>> observable = retrofitInterface.getFatherOrderList("commodity_order_list", user_id, state_flag, pageIndex);
        onSubscribe(observable, subscriber);
    }

    //获取子订单列表
    public void getChildOrderList(Subscriber<HttpResponse<List<OrderChildListBean>>> subscriber, String parent_order_id) {
        Observable<HttpResponse<List<OrderChildListBean>>> observable = retrofitInterface.getChildOrderList("commodity_children_order_list", parent_order_id);
        onSubscribe(observable, subscriber);
    }


    //订单支付
    //支付宝支付
    public void zfbPayment(Subscriber<HttpResponse<String>> subscriber, BigDecimal order_price, String order_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.zfb_payment("order_payment", 1, order_price, order_id);
        onSubscribe(observable, subscriber);
    }

    //微信支付
    public void vxPayment(Subscriber<HttpResponse<WxBean>> subscriber, BigDecimal order_price, String order_id) {
        Observable<HttpResponse<WxBean>> observable = retrofitInterface.vx_payment("order_payment", 2, order_price, order_id);
        onSubscribe(observable, subscriber);
    }

    //余额支付
    public void balancePayment(Subscriber<HttpResponse<String>> subscriber, BigDecimal order_price, String order_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.balance_payment("order_payment", 3, order_price, order_id);
        onSubscribe(observable, subscriber);
    }

    //线下扫码支付
    public void scanPayment(Subscriber<HttpResponse<String>> subscriber,String user_id, BigDecimal payment_amount, String store_str) {
        Observable<HttpResponse<String>> observable = retrofitInterface.scan_payment("order_scan_payment", user_id, payment_amount, store_str);
        onSubscribe(observable, subscriber);
    }

    //删除订单
    public void deleteOrder(Subscriber<HttpResponse<String>> subscriber, String parent_order_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.delete_order("commodity_delete_order", parent_order_id);
        onSubscribe(observable, subscriber);
    }

    //取消订单
    public void cancelOrder(Subscriber<HttpResponse<String>> subscriber, String parent_order_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.cancel_order("commodity_cancel_order", parent_order_id);
        onSubscribe(observable, subscriber);
    }

    //获取未评价商品(跳转订单评价页面)
    public void findAppraise(Subscriber<HttpResponse<AppraiseCommodityModel>> subscriber, String children_order_id) {
        Observable<HttpResponse<AppraiseCommodityModel>> observable = retrofitInterface.find_appraise("commodity_find_appraise_commodity", children_order_id);
        onSubscribe(observable, subscriber);
    }
    //提交订单评价
    public void orderAppraise(Subscriber<HttpResponse<String>> subscriber, String order_appraise) {
        Observable<HttpResponse<String>> observable = retrofitInterface.order_appraise("commodity_order_appraise", order_appraise);
        onSubscribe(observable, subscriber);
    }

    //确认收货
    public void confirmReceipt(Subscriber<HttpResponse<String>> subscriber, String parent_order_id) {
        Observable<HttpResponse<String>> observable = retrofitInterface.confirm_receipt("commodity_confirm_receipt", parent_order_id);
        onSubscribe(observable, subscriber);
    }

    //店铺内搜索
    public void shopSearch(Subscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber, Map<String,Object> map) {
        Observable<HttpResponse<List<CategoryGoodsDetailsBean>>> observable = retrofitInterface.shop_search(map);
        onSubscribe(observable, subscriber);
    }
}
